﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BYRBBS.Model;
using BYRBBS.Util;
using System.IO;
using System.Diagnostics;
using System.Xml.Linq;

namespace BYRForum
{
    public partial class EditArticle : PhoneApplicationPage
    {
        string replyTitle;
        string replyContent;

        public EditArticle()
        {
            InitializeComponent();
            TextReplyTitle.Text = App.ArticleList.Items[App.ArticleItemChoose].Article_Item.Title;
            TextReplyContent.Text = App.ArticleList.Items[App.ArticleItemChoose].Article_Item.Content;

        }

        private void ApplicationBarPost_Click(object sender, EventArgs e)
        {
            if (TextReplyTitle.Text == "" || TextReplyContent.Text == "")
            {
                MessageBox.Show("回复内容或标题不能为空！");
                return;
            }
            replyContent = TextReplyContent.Text;
            replyTitle = TextReplyTitle.Text;
            string url = ByrApi.apiUrl + "/article/" + App.ArticleList.Items[App.ArticleItemChoose].Article_Item.Board_name + "/update" + 
                "/" + App.ArticleList.Items[App.ArticleItemChoose].Article_Item.Id + ByrApi.Encode + ByrApi.appkey;
            HttpUtil.HttpPost(url, PostAriticle_Callback);
            progressBarWait.Visibility = Visibility.Visible;
        }
        private void PostAriticle_Callback(IAsyncResult asynchronousResult)
        {

            try
            {
                HttpWebRequest hwrq = (HttpWebRequest)asynchronousResult.AsyncState;

                Stream responseStream = hwrq.EndGetRequestStream(asynchronousResult);
                StreamWriter sr = new StreamWriter(responseStream);
                sr.Write("title=" + replyTitle + "&content=" + replyContent + "&signature=1");
                sr.Close();
                responseStream.Close();
                hwrq.BeginGetResponse(PostResponse_Callback, hwrq);
            }
            catch (Exception e)
            {
                Debug.WriteLine("POST STRING FAILED:" + e.Message);
            }
        }
        private void PostResponse_Callback(IAsyncResult asynchronousResult)
        {
            try
            {
                string result = HttpUtil.ReadToEnd(asynchronousResult);
                XDocument doc = XDocument.Parse(result);
                Debug.WriteLine(result);
                foreach (XElement item in doc.Descendants("error").Nodes())
                {
                    if (item.Name == "msg")
                    {
                        this.Dispatcher.BeginInvoke(
                            () =>
                            {
                                MessageBox.Show("回复失败：" + item.Value);
                                progressBarWait.Visibility = Visibility.Collapsed;

                            }
                            );


                        return;
                    }
                }
                this.Dispatcher.BeginInvoke(
                    () =>
                    {
                        MessageBox.Show("回复成功！");
                        progressBarWait.Visibility = Visibility.Visible;
                        this.NavigationService.GoBack();
                    }
                    );



            }
            catch { }



        }

        private void ApplicationBarCancel_Click(object sender, EventArgs e)
        {

        }
    }
}